"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 兼容M*数据库timestamp数据类型
Case Name   : timestamp类型结合distinct order by+group by+where+limit使用
Create At   : 2023.08.25
Owner       : z@wan005
Description :
    1、建表
    2、插入数据
    3、查询
    4、查询字段包含distinct结合group by使用
    5、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
    5、成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest29(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.db_name = "db_timestamp_0029"
        self.tb_name = "tb_timestamp_0029"

    def test_time(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：创建兼容b库---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '-step1:建表;expect:成功--'
        self.log.info(text)
        sql_cmd = f'''drop table if exists {self.tb_name};
        create table {self.tb_name}(ts1 timestamp ,ts2 timestamp, \
        ts3 timestamp);'''
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, sql_res,
                      '执行失败:' + text)

        text = '-step2:插入数据;expect:成功--'
        self.log.info(text)
        sql_cmd = f'''insert {self.tb_name} values('19011123102126',\
        '20011024152416.3','21551015235959.9999999');
        insert {self.tb_name} values('1902-12-31 23:59:59.9999999',\
        '08-11-30 23:59:59','541014100000');
        insert {self.tb_name} values('190909','08-11-30 23:59:59',\
        '21551015235959.9999999');
        insert {self.tb_name} values('294277-01-09 00:00:00',\
        '20011024152416.3','08-11-30 23:59:59');'''
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        self.assertEqual(4, sql_res.count('INSERT 0 1'), '执行失败:' + text)

        text = '-step3:查询;expect:成功--'
        self.log.info(text)
        sql_cmd = f"select * from {self.tb_name};"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        rows, _ = self.common.parse_sql_query_res(sql_res)
        expect = [['1901-11-23 10:21:26+08', '2001-10-24 15:24:16+08',
                   '2155-10-16 00:00:00+08'], ['1903-01-01 00:00:00+08',
                                               '2008-11-30 23:59:59+08',
                                               '2054-10-14 10:00:00+08'],
                  ['2019-09-09 00:00:00+08', '2008-11-30 23:59:59+08',
                   '2155-10-16 00:00:00+08'],
                  ['294277-01-09 00:00:00+08', '2001-10-24 15:24:16+08',
                   '2008-11-30 23:59:59+08']]
        self.assertEqual(rows, expect, '执行失败' + text)

        text = '-step4:查询字段包含distinct结合group by使用;expect:成功--'
        self.log.info(text)
        sql_cmd = f"select distinct ts2 from {self.tb_name} where " \
                  f"ts2>'20010101000000' group by ts2 order by 1 limit 3;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        res_list = ['2001-10-24 15:24:16+08', '2008-11-30 23:59:59+08']
        for res in res_list:
            self.assertIn(res, sql_res, '执行失败:' + text)

    def tearDown(self):
        text = '----step5:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname='postgres')
        self.log.info(drop_res1)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
